<?php


namespace Api\Controller;
use Think\Controller;
use Think\Model;
/**
 * 世果助理接口控制器
 * @author 曹梦龙 <138888611@qq.com>
 */
class AssistantController extends Controller {
    public function getCKDan(){
        $rq_q = I('rq_q');      //创建时间_前
        $rq_h = I('rq_h');      //创建时间_后
        $str = "";
        if($rq_q != null){
            $str .= " and lastupdate >= ".$rq_q;
        }
        if($rq_h != null){
            $str .= " and lastupdate <= ".$rq_h;
        }
        $sql="select * from gr_order where status = 2000".$str;
        $ckdanlist=M('order')->query($sql);
       
        for($i=0;$i<count($ckdanlist);$i++){
            $ckdanlist[$i]['totalamount']=$ckdanlist[$i]['useamount']+$ckdanlist[$i]['amount'];
            //$ckdanlist[$i]['detail']=M('vorder_goods')->where(array('orderid'=>$ckdanlist[$i]['orderid']))->select();
            $ckdanlist[$i]['detail']=M('order_goods')->where(array('orderid'=>$ckdanlist[$i]['orderid']))->select();
            for($b=0;$b<count($ckdanlist[$i]['detail']);$b++){ 
                $product=M('product')->where(array('id'=> $ckdanlist[$i]['detail'][$b]['pid']))->find(); 
                $ckdanlist[$i]['detail'][$b]['pname']=$product['name'];
                $ckdanlist[$i]['detail'][$b]['sku']=$product['sku'];
                $ckdanlist[$i]['detail'][$b]['quantity']=$product['quantity']; 
            }
         
        }
        //dump($ckdanlist);
        if($ckdanlist){
           $tags = array('code' => 0,'data'=>array('total' => count($ckdanlist),'rows' =>$ckdanlist));
            $this->ajaxReturn($tags);
        }else{
             $tags = array('code' => -1);
            $this->ajaxReturn($tags);
        }
    }
     public function getDGDan(){
        $rq_q = I('rq_q');      //创建时间_前
        $rq_h = I('rq_h');      //创建时间_后
        $str = "";
        if($rq_q != null){
            $str .= " and create_time >= ".$rq_q;
        }
        if($rq_h != null){
            $str .= " and create_time <= ".$rq_h;
        }
        $sql="select * from gr_agent_order where status = 2000".$str;
        $ckdanlist=M('agent_order')->query($sql);
        for($i=0;$i<count($ckdanlist);$i++){
            $sql="select a.orderid,b.pname,b.num from gr_agent_order a,gr_agent_order_goods b where a.orderid = b.orderid and a.orderid ='".$ckdanlist[$i]['orderid']."'";
            $res=M('agent_order_goods')->query($sql);
            $ckdanlist[$i]['detail']="";
            foreach ($res as $value){
                 $ckdanlist[$i]['detail'].=$value['pname']."--".$value['num']."_________  /  ";
            }
        }
        if($ckdanlist){
           $tags = array('code' => 0,'data'=>array('total' => count($ckdanlist),'rows' =>$ckdanlist));
           $this->ajaxReturn($tags);
        }else{
             $tags = array('code' => -1);
            $this->ajaxReturn($tags);
        }
    }
    public function getTHHDan(){
        $rq_q = I('rq_q');      //创建时间_前
        $rq_h = I('rq_h');      //创建时间_后
        $sql = "select a.*,"
            ." from_unixtime(a.createtime),"
            ."(select c.goods1 from gr_vreject_goods c where a.tdorderid = c.tdorderid)detail"
            ." from gr_reject a where 1 = 1";
        if($rq_q != null){
            $sql .= " and a.createtime >= ".$rq_q;
        }
        if($rq_h != null){
            $sql .= " and a.createtime <= ".$rq_h;
        }
        $model=new Model();
        $ckdanlist=$model->query($sql);
        if($ckdanlist){
           $tags = array('code' => 0,'data'=>array('total' => count($ckdanlist),'rows' =>$ckdanlist));
           $this->ajaxReturn($tags);
        }else{
           $tags = array('code' => -1);
           $this->ajaxReturn($tags);
        }
    }
}